package com.sxyjhh.fg.service;

import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonArrayFormatVisitor;
import com.sxyjhh.fg.dao.TwoBuilderInfoRepository;
import com.sxyjhh.fg.dao.TwoStructuralPersonInfoRepository;
import com.sxyjhh.fg.model.TwoBuilderInfo;
import com.sxyjhh.fg.model.TwoStructuralPersonInfo;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SaveDataService {

    private static final Logger logger = LoggerFactory.getLogger(SaveDataService.class);

    @Autowired
    private TwoBuilderInfoRepository twoBuilderInfoRepository;

    @Autowired
    private TwoStructuralPersonInfoRepository twoStructuralPersonInfoRepository;

    public boolean saveData(String regPersonType, String dataArray) {
        try {
            logger.info("receive data :"+regPersonType+"  "+dataArray);
            JSONArray jsonArray = new JSONArray(dataArray);//将结果转换成JSONArray对象的
            if ("06-02".equals(regPersonType)) {
                List<TwoBuilderInfo> dataList = new ArrayList();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = jsonArray.getJSONObject(i);
                    dataList.add(new TwoBuilderInfo(
                            jsonObj.getString("ID"),
                            jsonObj.getString("SEX"),
                            jsonObj.getString("NAME"),
                            jsonObj.getString("NATION"),
                            jsonObj.getString("CARDID"),
                            jsonObj.getString("PROFESSIONNAME"),
                            jsonObj.getString("CODE"),
                            "二级注册建造师",
                            jsonObj.getString("CERTIFICATEENT"),
                            jsonObj.getString("USERFULLDATE"),
                            jsonObj.getString("STATUS"),
                            new Date()));
                }
                twoBuilderInfoRepository.save(dataList);

            } else if ("12".equals(regPersonType)) {
                List<TwoStructuralPersonInfo> dataList = new ArrayList();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObj = jsonArray.getJSONObject(i);
                    dataList.add(new TwoStructuralPersonInfo(
                            jsonObj.getString("ID"),
                            jsonObj.getString("SEX"),
                            jsonObj.getString("NAME"),
                            jsonObj.getString("NATION"),
                            jsonObj.getString("CARDID"),
                            jsonObj.getString("PROFESSIONNAME"),
                            jsonObj.getString("CODE"),
                            "二级注册结构工程师",
                            jsonObj.getString("CERTIFICATEENT"),
                            jsonObj.getString("STATUS"),
                            new Date()));
                }
                twoStructuralPersonInfoRepository.save(dataList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("saveData",e);
            return false;
        }
        return true;
    }

}
